# Create your models here.


from django.db import models

from Apps.Project.models.project import Project
from Apps.common.custommodel import ModifyModel, CustomModel
from Apps.user.models import User
import datetime


class uiCase(CustomModel):
    """
    录制脚本的存储
    """
    case_name = models.CharField(max_length=50, default="", verbose_name="用例名称")
    case_url = models.CharField(max_length=50, default="", verbose_name="用例存储的url地址")
    case_data = models.TextField(default="", verbose_name="脚本的JSON原始数据")
    is_delete = models.BooleanField(default=False, verbose_name="用例是否删除")
    case_description = models.CharField(max_length=50, default="", verbose_name="用例描述")
    project = models.ForeignKey(to=Project, on_delete=models.DO_NOTHING, db_constraint=False)
    creator = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="uiCase_c", db_constraint=False,
                                verbose_name="创建人")
    modifier = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="uiCase_m", db_constraint=False,
                                 verbose_name="修改人")

    class Meta:
        db_table = "uicase"
        verbose_name = "uicase"
        ordering = ('-create_time',)

    def __str__(self):
        return self.case_name


class uiCaseLog(CustomModel):
    """
    执行用例的日志
    """

    browserName = models.CharField(max_length=20, default="--", verbose_name="浏览器名称")
    platformName = models.CharField(max_length=20, default="--", verbose_name="PC 执行的平台")
    platform = models.CharField(max_length=20, default="ANY", verbose_name="移动端 执行的平台")
    session_id = models.CharField(max_length=50, default="", verbose_name="运行时的session_id")
    capabilities = models.JSONField(default="", verbose_name="对应每个执行节点的属性Json")
    run_case_data = models.TextField(default="", verbose_name="执行用例时，脚本的Json数据")
    run_case_data_tests = models.TextField(default="", verbose_name="测试步骤数据，用于前端展示用")
    current_test_id = models.CharField(max_length=50, default="", verbose_name="当前正在执行节点的ID")
    echo_text = models.JSONField(default="", verbose_name="测试过程中，echo输出的值")

    # 真正执行的时间，因为点击执行时，可能在排队等待执行
    start_time = models.DateTimeField(auto_now_add=True, verbose_name="用例开始执行的时间")
    end_time = models.DateTimeField(auto_now_add=True, verbose_name="用例结束执行的时间")
    elapsed_time = models.CharField(max_length=16, default=0, verbose_name="执行总耗时")
    is_success = models.CharField(max_length=2, default="1", verbose_name="是否成功,1-未执行，2-成功，3-失败")
    status = models.CharField(max_length=2, default="1", verbose_name="用例是否开始执行状态,1-未执行，2-正在执行，3-已执行")
    error_message = models.CharField(max_length=256, default="", verbose_name="错误描述")
    img_url = models.JSONField(default="", verbose_name="每个步骤执行后，保存的图片地址")
    case = models.ForeignKey(to=uiCase, on_delete=models.CASCADE, db_constraint=False)
    Project = models.CharField(default="", max_length=50, verbose_name='项目ID')
    creator = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="uiCaseLog_c", db_constraint=False,
                                verbose_name="创建人")
    modifier = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="uiCaseLog_m", db_constraint=False,
                                 verbose_name="修改人")

    class Meta:
        db_table = "uicaselog"
        verbose_name = "uicaselog"
        ordering = ('-create_time',)

    def __str__(self):
        return self.case


class nodeConfig(CustomModel):
    """
    执行用例的日志
    """
    nodeName = models.CharField(max_length=30, default="", verbose_name="节点名称")
    nodeAddr = models.CharField(max_length=50, default="", verbose_name="节点地址")
    nodePort = models.CharField(max_length=10, default="", verbose_name="节点端口")
    nodeOs = models.CharField(max_length=30, default="", verbose_name="操作系统")
    nodeStatus = models.CharField(max_length=30, default="", verbose_name="节点状态")
    nodeMaxSessions = models.IntegerField(max_length=10, verbose_name="最大Session数")
    nodeUseSessions = models.IntegerField(max_length=10, verbose_name="已使用Session数")
    nodeElapsedTime = models.IntegerField(max_length=10, verbose_name="当前运行时长")
    nodeLastElapsedTime = models.IntegerField(max_length=10, verbose_name="上次运行时长")

    creator = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="nodeConfig_c", db_constraint=False,
                                verbose_name="创建人")
    modifier = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="nodeConfig_m", db_constraint=False,
                                 verbose_name="修改人")

    class Meta:
        db_table = "nodeconfig"
        verbose_name = "nodeconfig"
        ordering = ('-create_time',)

    def __str__(self):
        return self.nodeName
